package pl.cba.solgar.game.rogal;

public class PriorityQueue {
	private QueueElement top;

	public PriorityQueue() {

	}

	public boolean empty() {
		return top == null;
	}

	public void push(QueueElement element) {

		if (top == null) {
			top = element;
		}
		else {
			QueueElement previous = null;
			QueueElement current = top;
			while (element.priority > current.priority) {
				if (current.next == null) {
					current.next = element;
					return;
				}
				previous = current;
				current = current.next;
			}
			if (current == top) {
				element.next = top;
				top = element;
			}
			else {
				previous.next = element;
				element.next = current;
			}
		}
	}

	public QueueElement pop() {
		if (top == null)
			return null;
		QueueElement el = top;
		top = el.next;
		return el;
	}
}
